package com.swpu.service.impl;

import com.swpu.mapper.BrandMapper;
import com.swpu.pojo.Brand;
import com.swpu.pojo.PageBean;
import com.swpu.service.BrandService;
import com.swpu.util.SqlSessionFactoryUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class BrandServiceImpl implements BrandService {
    SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();

    @Override
    public List<Brand> selectAll() {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        List<Brand> brands = mapper.selectAll();

        sqlSession.close();
        return brands;
    }

    @Override
    public void add(Brand brand) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        mapper.add(brand);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public void deleteByIds(int[] ids){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        mapper.deleteByIds(ids);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public PageBean<Brand> selectByPage(int currentPage, int pageSize) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        int begin = (currentPage - 1)*pageSize;
        List<Brand> brands =  mapper.selectByPage(begin,pageSize);
        int totalCount = mapper.selectTotalCount();

        PageBean<Brand> pageBean = new PageBean<>();
        pageBean.setTotalCount(totalCount);
        pageBean.setRow(brands);

        sqlSession.close();
        return pageBean;
    }

    @Override
    public PageBean<Brand> selectByPageAndCondition(int currentPage, int pageSize,Brand brand) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        int begin = (currentPage - 1)*pageSize;
        List<Brand> brands =  mapper.selectByPageAndCondition(begin,pageSize,brand);

        PageBean<Brand> pageBean = new PageBean<>();
        pageBean.setRow(brands);

        sqlSession.close();
        return pageBean;
    }

    @Override
    public int selectTotalCountByCondition(Brand brand){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        //设置模糊表达式
        String brandName = brand.getBrandName();
        String companyName = brand.getCompanyName();
        Integer status = brand.getStatus();

        if (status!=null||(brandName!=null && brandName.length()>0)||(companyName!=null && companyName.length()>0)){
            if (brandName!=null && brandName.length()>0){
                brand.setBrandName("%"+brandName+"%");
            }
            if (companyName!=null && companyName.length()>0){
                brand.setCompanyName("%"+companyName+"%");
            }
            int totalCount = (int) mapper.selectTotalCountByCondition(brand);
            return totalCount;
        }

        int totalCount = (int) mapper.selectTotalCount();
        return totalCount;
    }

}
